package com.example.rice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.rice.common.CustomException;
import com.example.rice.entity.Category;
import com.example.rice.entity.Dish;
import com.example.rice.entity.Setmeal;
import com.example.rice.mapper.CategoryMapper;
import com.example.rice.service.CategoryService;
import com.example.rice.service.DishService;
import com.example.rice.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类，查询是否还存在该分类下的数据，有的话不能删除
     * @param id
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        Long count = dishService.count(dishLambdaQueryWrapper);

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        Long count1 = setmealService.count(setmealLambdaQueryWrapper);
        if (count1>0){
            //还存在关联需要返回异常，给用户提示
            throw new CustomException("当前分类下还存在关联套餐，不能删除");
        }
        if (count>0){
            //还存在关联需要返回异常，给用户提示
            throw new CustomException("当前分类下还存在关联菜品，不能删除");
        }
        //调用父类方法删除分类
        super.removeById(id);
    }
}
